import { Component, OnInit } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { CmHttp } from "http-module";
import { BASEURL, STOCKURL } from "shared";

@Component({
  // moduleId: module.id,
  selector: 'basic-info',
  templateUrl: 'basic-info.component.html',
  styleUrls: ['basic-info.component.less'],
  inputs: [
    "quoteSource" //包含quote信息的Observable
  ]
})
export class BasicInfoComponent implements OnInit {
  stockInfo;
  quoteSource;
  infoHeader: { key: string, label: string, unit?: string }[] = [
    {
      label: "总股本",
      key: "total_shares"
    },
    {
      label: "流通A股",
      key: "a_floats"
    },
    {
      label: "每股收益",
      key: "eps"
    },
    {
      label: "每股净资产",
      key: "bps"
    },
    {
      label: "净资产收益率",
      key: "roe"
    },
    {
      label: "市盈率",
      key: "pe"
    },
    {
      label: "每股经营性现金",
      key: "oper_cash_flowps"
    },
    {
      label: "每股公积金",
      key: "accumulation_fundps"
    },
    {
      label: "每股未分配利润",
      key: "undivided_profit"
    },
    {
      label: "净利润同比",
      key: "net_profit_growrate"
    },
    {
      label: "营业收入同比",
      key: "oper_revenue_growrate",
      unit: "%"
    },
    {
      label: "市净率",
      key: "pb"
    }
  ]

  constructor(private http: CmHttp) { }

  ngOnInit() {
    let { quoteSource, http} = this;
    quoteSource.filter(quote => quote["symbol"]).switchMap(quote =>
      Observable.zip(
        http.get(`${STOCKURL}/api/v2/sc/stocks/f10/${quote["symbol"]}/headlines`, {}, {cacheAge: http.CACHE_AGE.Instant}),
        http.get(`${STOCKURL}/api/v2/sc/charts/${quote["symbol"]}/summary`, {}, {cacheAge: http.CACHE_AGE.Instant}),
        (fData, detail) => Object.assign({}, detail.data, fData.data)
      )
    ).subscribe(stockInfo => this.stockInfo = stockInfo);
  }

}